package cn.shyouchi.wuguanjia.android.ui.event.checknum;

import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.ui.event.checkpoint.CheckPointEntity;
import cn.shyouchi.wuguanjia.android.utils.ToastUtil;
import cn.shyouchi.wuguanjia.android.widget.dialog.TimePickerDialogFragment;

/**
 * Created by Eenie on 2016/11/18 at 14:37
 * Email: 472279981@qq.com
 * Des:
 */

public class SelCheckPointAdapter extends BaseQuickAdapter<CheckPointEntity.ResultMessageBean,BaseViewHolder> {

    FragmentManager mFragmentManager;

    public SelCheckPointAdapter(FragmentManager mFragmentManager, List<CheckPointEntity.ResultMessageBean> data) {
        super(R.layout.item_event_sel_check_point_layout, data);
        this.mFragmentManager = mFragmentManager;
    }

    @Override
    protected void convert(final BaseViewHolder holder, final CheckPointEntity.ResultMessageBean bean) {
        holder.setText(R.id.tv_position, String.valueOf(holder.getPosition() + 1));
        CheckBox checkBox = holder.getView(R.id.ck_state);
        checkBox.setChecked(bean.isCheck());

        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bean.setCheck(!bean.isCheck());
                notifyItemChanged(holder.getPosition());
            }
        });

        holder.setText(R.id.tv_point_name, bean.getInspectionname());
        holder.setText(R.id.tv_point_time, TextUtils.isEmpty(bean.getTime()) || !bean.isCheck() ? "巡检时间" : bean.getTime());
        holder.getView(R.id.tv_point_time).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bean.isCheck()) {
                    showTimePicker(bean, "选择巡检时间", holder.getPosition());
                } else {
                    ToastUtil.showToast("请先勾选该点位");
                }
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bean.setCheck(!bean.isCheck());
                notifyItemChanged(holder.getPosition());
            }
        });
    }

    public void showTimePicker(final CheckPointEntity.ResultMessageBean bean, String title, final int position) {
        TimePickerDialogFragment mTimePickerDialog;
        mTimePickerDialog = new TimePickerDialogFragment(mFragmentManager, title, "");
        mTimePickerDialog.setOnTimePickedListener(new TimePickerDialogFragment.onTimePickedListener() {
            @Override
            public void onPicked(String time) {
                bean.setTime(time);
                notifyItemChanged(position);
            }
        });
        mTimePickerDialog.showDialog();
    }
}
